import { defineStore } from "pinia";
import axios from "axios";
import { showSuccessToast, showFailToast } from "vant";
import router from "../router/router";

export const useUsersStore1 = defineStore("login", {
  state() {
    return {
      userName: "",
      userPwd: "",
    };
  },
  actions: {
    logn() {
      axios
        .post("/user/login", { userName: this.userName, userPwd: this.userPwd })
        .then((res) => {
          //   console.log(res);
          if (res.data.code !== 0) return showFailToast("用户名或密码错误");
          showSuccessToast("登入成功");
          window.sessionStorage.setItem("userID", res.data.data.id);
          window.sessionStorage.setItem("token", res.data.myToken);
          router.push("/my");
        });
    },
  },
});
